package com.ljq.rpc.client.proxy;

import com.ljq.rpc.client.config.NettyClient;
import com.ljq.rpc.client.utils.MyBeanUtils;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.stereotype.Component;

import java.lang.reflect.Proxy;

public class ProxyFactory<T> implements FactoryBean<T> {

    /**
     * 构建DefaultRepository需要使用的参数
     */
    private Class<T> interfaceType;

    public ProxyFactory(Class<T> interfaceType) {
        this.interfaceType = interfaceType;
    }

    @Override
    public T getObject() throws Exception {
        // 因为DefaultRepository需要Class<T>作为参数,所以该类包含一个Claa<T>的成员,通过构造函数初始化
        NettyClient nettyClient = MyBeanUtils.getBean(NettyClient.class);
        return (T) nettyClient.getBean(interfaceType);
    }

    @Override
    public Class<?> getObjectType() {
        // 该方法返回的getObject()方法返回对象的类型,这里是基于interfaceType生成的代理对象,所以类型就是interfaceType
        return interfaceType;
    }
}